/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.fund.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.fund.domain.UserStock;
import me.zhengjie.modules.fund.service.UserStockService;
import me.zhengjie.modules.fund.service.dto.UserStockDto;
import me.zhengjie.modules.fund.service.dto.UserStockQueryCriteria;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Set;

/**
 * @author yuchuan.wang
 * @date 2021/06/22
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "工具：我的股票")
@RequestMapping("/api/userstock")
public class UserStockController {

    private final UserStockService userStockService;

    private static final String ENTITY_NAME = "userstock";

    @ApiOperation("查询我的股票")
    @GetMapping
    @PreAuthorize("@el.check('userstock:list')")
    public ResponseEntity<Object> query(UserStockQueryCriteria criteria, Pageable pageable) {
        criteria.setUserId(SecurityUtils.getCurrentUserId());
        Page<UserStockDto> pageUserStocks = userStockService.queryAll(criteria, pageable);
        return new ResponseEntity<>(PageUtil.toPage(pageUserStocks), HttpStatus.OK);
    }

    @Log("新增我的股票")
    @ApiOperation("新增我的股票")
    @PostMapping
    @PreAuthorize("@el.check('userstock:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody UserStock resources) throws IOException {
        if (resources.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        resources.setUserId(SecurityUtils.getCurrentUserId());
        userStockService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改我的股票")
    @ApiOperation("修改我的股票")
    @PutMapping
    @PreAuthorize("@el.check('userstock:edit')")
    public ResponseEntity<Object> update(@Validated(UserStock.Update.class) @RequestBody UserStock resources) throws IOException {
        userStockService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除我的股票")
    @ApiOperation("删除我的股票")
    @DeleteMapping
    @PreAuthorize("@el.check('userstock:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids) {
        // 验证是否被用户关联
        userStockService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
